package com.doctorliang.cheng.shopping.shoppintcar.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.doctorliang.cheng.shopping.R;
import com.doctorliang.cheng.shopping.home.bean.GoodsBean;
import com.doctorliang.cheng.shopping.shoppintcar.utils.CartStorage;
import com.doctorliang.cheng.shopping.shoppintcar.view.NumberAddSubView;
import com.doctorliang.cheng.shopping.utils.Constants;

import java.util.List;

/**
 * Created by CHENG on 2017/10/1.
 * 购物车recycleview的适配器
 */

public class ShowShoppingAdapter extends RecyclerView.Adapter {
    private final Context mcontext;
    private final List<GoodsBean> goodsBeanList;
    private final CheckBox checkboxAll;
    private final TextView tvShopcartTotal;
    /**
     * 删除状态下的checkbox全选框
     */
    private final CheckBox cbAll;

    public ShowShoppingAdapter(Context mcontext, List<GoodsBean> goodsBeanList, CheckBox checkboxAll, TextView tvShopcartTotal, CheckBox cbAll) {
        this.mcontext = mcontext;
        this.goodsBeanList = goodsBeanList;
        this.checkboxAll = checkboxAll;
        this.tvShopcartTotal = tvShopcartTotal;
        this.cbAll = cbAll;
        //设置Item监听
        setListener();
        //显示总价格
        showTotalPrice();
        //校验是否全选状态
        checkAll();

    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHodler(View.inflate(mcontext, R.layout.item_shop_cart,null));
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
         ViewHodler viewHodler = (ViewHodler) holder;
        viewHodler.setData(goodsBeanList.get(position),position);
    }

    @Override
    public int getItemCount() {
        return goodsBeanList.size();
    }
    public void showTotalPrice(){
     tvShopcartTotal.setText(""+getTotalPrice());
    }

    private double getTotalPrice() {
        double total = 0.0;
        if (goodsBeanList!=null&&goodsBeanList.size()>0) {
            for (int i=0;i<goodsBeanList.size();i++) {
                GoodsBean goodsBean = goodsBeanList.get(i);
                if (goodsBean.isChildSelected()) {
                    total = total + Double.valueOf(goodsBean.getCover_price()) * Double.valueOf(goodsBean.getNumber());
                }
            }
        }
        return total;
    }

    public void delete() {
        if (goodsBeanList!=null&&goodsBeanList.size()>0) {
            for (int i=0;i<goodsBeanList.size();i++) {
                GoodsBean goodsBean = goodsBeanList.get(i);
                if (goodsBean.isChildSelected()) {
                    //1内存删除
                    goodsBeanList.remove(goodsBean);
                    //2本地删除
                    CartStorage.getInstance().deleteData(goodsBean);
                    //3刷新
                    notifyItemRemoved(i);
                    //每删除一次就i--
                    i--;
                }
            }
        }
    }

    private class ViewHodler extends RecyclerView.ViewHolder {
        private CheckBox cbGov;
        private ImageView ivGov;
        private TextView tvDescGov;
        private TextView tvPriceGov;
        private NumberAddSubView numberAddSubView;

        public ViewHodler(View view) {
            super(view);
            cbGov = (CheckBox) itemView.findViewById(R.id.cb_gov);
            ivGov = (ImageView) itemView.findViewById(R.id.iv_gov);
            tvDescGov = (TextView) itemView.findViewById(R.id.tv_desc_gov);
            tvPriceGov = (TextView) itemView.findViewById(R.id.tv_price_gov);
            numberAddSubView = (NumberAddSubView) itemView.findViewById(R.id.numberAddSubView);
            //设置recycleview的每条item的监听
            view.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (onitemClickListener!=null) {
                        onitemClickListener.onItemclick(getLayoutPosition());
                    }
                }
            });
        }

        public void setData(final GoodsBean goodsBean, final int position) {
            //设置cb
            cbGov.setChecked(goodsBean.isChildSelected());
            //设置图片
            Glide.with(mcontext).load(Constants.BASE_URl_IMAGE+goodsBean.getFigure()).into(ivGov);
            //设置详情
            tvDescGov.setText(goodsBean.getName());
            //设置价格
            tvPriceGov.setText(goodsBean.getCover_price());
            //设置商品的数量
            numberAddSubView.setValue(goodsBean.getNumber());
            //设置库存的最大最小值
            numberAddSubView.setMinValue(1);
            numberAddSubView.setMaxValue(8);
            //设置商品数量变化的监听
            numberAddSubView.setOnNumberChangeListener(new NumberAddSubView.OnNumberChangeListener() {
                @Override
                public void onNumberChange(int value) {
                    //1更新当前列表数量
                    goodsBean.setNumber(value);
                    //2本地要更新
                    CartStorage.getInstance().updataData(goodsBean);
                    //3刷新适配器
                    notifyItemChanged(position);
                    //4再次计算总价格
                    showTotalPrice();
                }
            });
            //checkBoxAll的点击事件
            checkboxAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //1得到全选框的状态
                    boolean checked = checkboxAll.isChecked();
                    //2根据状态设置全选还是非全选
                    check_all_none(checked);
                    //3计算总价格
                    showTotalPrice();
                }
            });
            //cbAll的点击事件
            cbAll.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    //1得到全选框的状态
                    boolean checked = cbAll.isChecked();
                    //2根据状态设置全选还是非全选
                    check_all_none(checked);
                }
            });
        }
    }

    public void check_all_none(boolean checked) {
        if (goodsBeanList!=null&&goodsBeanList.size()>0) {
            for (int i=0;i<goodsBeanList.size();i++) {
                GoodsBean goodsBean = goodsBeanList.get(i);
                //把得到的状态设置进bean对象去(因为刚才的监听也设置过了，所以只需要再次刷新就会检验)
                goodsBean.setChildSelected(checked);
                //更新适配器
                notifyItemChanged(i);
            }
        }
    }

    private void setListener() {
        setOnitemClickListener(new OnitemClickListener() {
            @Override
            public void onItemclick(int position) {
                //1根据位置找到对应的bean对象
                GoodsBean goodsBean = goodsBeanList.get(position);
                //2设置取反状态
                goodsBean.setChildSelected(!goodsBean.isChildSelected());
                //3刷新状态
                notifyItemChanged(position);
                //校验是否全选
                checkAll();
                //5从新计算价格
                showTotalPrice();
            }
        });
    }

    /**
     * 校验是否全选 这里面包含了编辑状态下的和完成状态下的checkbox
     */
    public void checkAll() {
        int num = 0;
        if (goodsBeanList != null && goodsBeanList.size() > 0) {
            for (int i = 0; i < goodsBeanList.size(); i++) {
                GoodsBean goodsBean = goodsBeanList.get(i);
                if (!goodsBean.isChildSelected()) {
                    //非全选，设置全选按钮为false
                    checkboxAll.setChecked(false);
                    cbAll.setChecked(false);
                } else {
                    //累加有选中的
                    num++;
                }
            }
            if (num == goodsBeanList.size()) {
                //是全选,设置全选按钮为true
                checkboxAll.setChecked(true);
                cbAll.setChecked(true);
            }
        } else {
            //非全选，设置全选按钮为false
            checkboxAll.setChecked(false);
            cbAll.setChecked(false);
        }
    }

    /**
     * 点击recycleview中item的监听
     */
    public interface OnitemClickListener{
        /**
         * 点击时回调一个位置
         * @param position
         */
        public void onItemclick(int position);
    }
    private OnitemClickListener onitemClickListener;

    /**
     * 供外界或内部使用
     * @param onitemClickListener
     */
    public void setOnitemClickListener(OnitemClickListener onitemClickListener) {
        this.onitemClickListener = onitemClickListener;
    }
}
